package com.luckyframe.project.jmeter;

import com.luckyframe.common.utils.StringUtils;
import com.luckyframe.framework.config.JmeterConfig;
import com.luckyframe.project.jmeter.execution.JmeterExecutor;
import org.apache.jmeter.JMeter;
import org.apache.jmeter.util.JMeterUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

@Component
public class JmeterInitializer implements InitializingBean {

    private static final Logger log = LoggerFactory.getLogger(JmeterInitializer.class);

    @Autowired
    JmeterConfig jmeterConfig;

    @Override
    public void afterPropertiesSet() throws Exception {
        String JMETER_HOME = System.getProperty("jmeter.home");
        if(StringUtils.isEmpty(JMETER_HOME)){
            throw new RuntimeException("jmeter.home参数未设置，请通过-D方式设置参数");
        }

        
        log.info("jmeter home={}",JMETER_HOME);

        String JMETER_PROPERTIES = JMETER_HOME + "/bin/jmeter.properties";
        JMeterUtils.loadJMeterProperties(JMETER_PROPERTIES);
        JMeterUtils.setJMeterHome(JMETER_HOME);
        JMeterUtils.setLocale(LocaleContextHolder.getLocale());
        JMeterUtils.setProperty(JMeter.JMETER_REPORT_OUTPUT_DIR_PROPERTY,jmeterConfig.getReportGenDir());
    }
}
